"""
配置文件 - 集中管理API认证token和其他配置信息
"""

# API认证配置
# 尝试从用户输入获取access_token，如果没有输入则使用默认值
def get_access_token_from_input():
    """从用户输入获取access_token"""
    default_token = "7d89782b-fccd-46c9-aaba-5b387e50aa18_68ea1124e01817302f98fea5_client"
    token = input(f"请输入access_token的值 [默认值将被使用]: ")
    return token if token else default_token

API_CONFIG = {
    # 通用access-token
    "access_token": get_access_token_from_input(),
    # 可选的替代token (用户提供的新token)
    "alternative_token": "6c389a7e-caa2-4348-8ad8-39f55b82b2fa_68e77334e01817302f91ade1_client",
    # 默认使用的token键名
    "active_token": "access_token",  # 可以切换为 "alternative_token"
    
    # API基本配置
    "base_url": "https://app.molardata.com",
    "api_version": "v2",
    
    # 默认请求超时时间（秒）
    "timeout": 60,
    
    # 默认用户代理
    "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36",
    
    # 默认语言设置
    "language": "zh-CN",
    "timezone": "Asia/Shanghai"
}

# 获取当前激活的token
def get_active_token():
    """
    获取当前激活的access-token
    
    Returns:
        str: 当前激活的access-token值
    """
    token_key = API_CONFIG["active_token"]
    return API_CONFIG.get(token_key, API_CONFIG["access_token"])

# 切换token
def switch_token(token_key="alternative_token"):
    """
    切换使用的token
    
    Args:
        token_key (str): token键名，"access_token" 或 "alternative_token"
    
    Returns:
        bool: 切换是否成功
    """
    if token_key in ["access_token", "alternative_token"]:
        API_CONFIG["active_token"] = token_key
        print(f"已切换到{token_key}，当前token: {get_active_token()[:10]}****{get_active_token()[-4:]}")
        return True
    else:
        print(f"无效的token键名: {token_key}，必须是 'access_token' 或 'alternative_token'")
        return False

# 获取默认请求头
def get_default_headers(task_id=None, batch_id=None):
    """
    获取默认的API请求头
    
    Args:
        task_id (str, optional): 任务ID，用于构建Referer
        batch_id (str, optional): 批次ID，用于构建Referer
    
    Returns:
        dict: 默认的请求头字典
    """
    # 构建Referer
    referer = f"https://app.molardata.com/task-v2/data-item?"
    if task_id:
        referer += f"taskId={task_id}&"
    if batch_id:
        referer += f"vm=batch&batchId={batch_id}&"
    if task_id or batch_id:
        referer += "dm=all"
    else:
        # 如果没有提供taskId和batchId，使用默认值
        referer += "taskId=68d255e332a0f5d164c7081a&vm=batch&batchId=68d28091dd785cd49b388c61&dm=all"
    
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": API_CONFIG["language"],
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "Origin": API_CONFIG["base_url"],
        "Pragma": "no-cache",
        "Referer": referer,
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": API_CONFIG["user_agent"],
        "X-Timezone": API_CONFIG["timezone"],
        "access-token": get_active_token(),
        "language": API_CONFIG["language"],
        "sec-ch-ua": '"Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"',
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": '"Windows"',
        "trace-id": f"{get_random_trace_id()}"
    }
    
    return headers

def get_random_trace_id():
    """
    生成随机的trace-id
    
    Returns:
        str: 随机的trace-id字符串
    """
    import uuid
    return str(uuid.uuid4())

# 打印当前配置信息
def print_config_info():
    """
    打印当前配置信息（不显示完整token）
    """
    token = get_active_token()
    masked_token = f"{token[:10]}****{token[-4:]}"
    
    print("===== API配置信息 =====")
    print(f"基础URL: {API_CONFIG['base_url']}")
    print(f"API版本: {API_CONFIG['api_version']}")
    print(f"活动token: {API_CONFIG['active_token']}")
    print(f"access-token: {masked_token}")
    print(f"超时时间: {API_CONFIG['timeout']}秒")
    print(f"默认语言: {API_CONFIG['language']}")
    print(f"时区设置: {API_CONFIG['timezone']}")
    print("======================")